package com.ruoyi.web.controller.mch;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.mch.bo.WxMchConfigAddBo;
import com.ruoyi.mch.bo.WxMchConfigEditBo;
import com.ruoyi.mch.bo.WxMchConfigQueryBo;
import com.ruoyi.mch.service.IWxMchConfigService;
import com.ruoyi.mch.vo.WxMchConfigVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 微信商户配置Controller
 *
 * @author ruoyi
 * @date 2021-06-02
 */
@Api(value = "微信商户配置控制器", tags = {"微信商户配置管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mch/config")
public class WxMchConfigController extends BaseController {

    private final IWxMchConfigService iWxMchConfigService;

    /**
     * 查询微信商户配置列表
     */
    @ApiOperation("查询微信商户配置列表")
    @PreAuthorize("@ss.hasPermi('mch:config:list')")
    @GetMapping("/list")
    public TableDataInfo<WxMchConfigVo> list(WxMchConfigQueryBo bo) {
        startPage();
        List<WxMchConfigVo> list = iWxMchConfigService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 查询微信商户配置列表
     */
    @ApiOperation("查询所有微信商户配置列表")
    @GetMapping("/all")
    public AjaxResult<List<WxMchConfigVo>> all() {
        List<WxMchConfigVo> list = iWxMchConfigService.queryList(new WxMchConfigQueryBo());
        return AjaxResult.success(list);
    }

    /**
     * 导出微信商户配置列表
     */
    @ApiOperation("导出微信商户配置列表")
    @PreAuthorize("@ss.hasPermi('mch:config:export')")
    @Log(title = "微信商户配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<WxMchConfigVo> export(WxMchConfigQueryBo bo) {
        List<WxMchConfigVo> list = iWxMchConfigService.queryList(bo);
        ExcelUtil<WxMchConfigVo> util = new ExcelUtil<WxMchConfigVo>(WxMchConfigVo.class);
        return util.exportExcel(list, "微信商户配置");
    }

    /**
     * 获取微信商户配置详细信息
     */
    @ApiOperation("获取微信商户配置详细信息")
    @PreAuthorize("@ss.hasPermi('mch:config:query')")
    @GetMapping("/{configId}")
    public AjaxResult<WxMchConfigVo> getInfo(@PathVariable("configId" ) Long configId) {
        return AjaxResult.success(iWxMchConfigService.queryById(configId));
    }

    /**
     * 新增微信商户配置
     */
    @ApiOperation("新增微信商户配置")
    @PreAuthorize("@ss.hasPermi('mch:config:add')")
    @Log(title = "微信商户配置", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody WxMchConfigAddBo bo) {
        return toAjax(iWxMchConfigService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改微信商户配置
     */
    @ApiOperation("修改微信商户配置")
    @PreAuthorize("@ss.hasPermi('mch:config:edit')")
    @Log(title = "微信商户配置", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody WxMchConfigEditBo bo) {
        return toAjax(iWxMchConfigService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除微信商户配置
     */
    @ApiOperation("删除微信商户配置")
    @PreAuthorize("@ss.hasPermi('mch:config:remove')")
    @Log(title = "微信商户配置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] configIds) {
        return toAjax(iWxMchConfigService.deleteWithValidByIds(Arrays.asList(configIds), true) ? 1 : 0);
    }
}
